#include "configdialog.h"
#include "../src/CManageNetwork.h"

ConfigDialog::ConfigDialog(QWidget *parent)
: QDialog(parent), ui(new Ui::Dialog)
{
    ui->setupUi(this);

	qRegisterMetaType<UpdateWhat>("UpdateWhat");

	QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(SendChatMessage()));
	QObject::connect(ui->pushButtonConnect,SIGNAL(clicked()),this,SLOT(AttemptConnection()));
	QObject::connect(ui->pushButtonCreateServer,SIGNAL(clicked()),this,SLOT(AttemptServerStart()));
	QObject::connect(this,SIGNAL(SendUpdateGUI(const char* , UpdateWhat)),this,SLOT(UpdateGUI(const char *, UpdateWhat)),Qt::QueuedConnection);

	ThisUI = ui;

	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),0,1); // fwd
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),1,1); // bkfwd
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),2,1); // left
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),3,1); // right
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),4,1); // up
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),5,1); // down
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),6,1); // shoot
	ui->LayoutKeyLayout->addWidget(new QLineEditEx(),7,1); // menu

	CManageNetworkSingle::getInstance()->m_pServer->SetCallbackGUI(this);
	CManageNetworkSingle::getInstance()->m_pClient->SetCallbackGUI(this);
	strYes = "Ja";
	strNo = "Nein";

}

ConfigDialog::~ConfigDialog()
{
    delete ui;
}

void ConfigDialog::SendChatMessage()
{
	// get the chat string
	QString str = ui->lineEdit_ChatLine->text();

	const char* Message;

	// convert
	QByteArray MessageByteArray = str.toAscii();
	Message = MessageByteArray.data();

	// send to client (which sends the message via rpc)
	CManageNetworkSingle::getInstance()->m_pClient->SendChatMessage(Message);
}

void ConfigDialog::UpdateGUI(const char* text, UpdateWhat uwUpdate)
{
	QString Text(text);
	QString DisplayText;

	switch(uwUpdate)
	{
	case ServerLog:
		DisplayText = ui->plainTextServerLog->toPlainText();
		DisplayText += Text;
		ui->plainTextServerLog->setPlainText(DisplayText);
		break;
	case ChatWin:
		DisplayText = ui->plainTextChatWin->toPlainText();
		DisplayText += Text;
		ui->plainTextChatWin->setPlainText(DisplayText);
		break;
	}
}

void ConfigDialog::AttemptConnection()
{
	const char* Server;
	unsigned int RemotePort;

	QLineEdit* qualleServer = ThisUI->lineEditServer;
	QLineEdit* quallePort = ThisUI->lineEditPort;
	// get data from fields
	QString qsServer = qualleServer->text();
	QString qsPort = quallePort->text();
	
	// convert
	QByteArray serverByteArray = qsServer.toAscii();
	Server = serverByteArray.data();

	bool ok = false;
	RemotePort = qsPort.toUShort(&ok,10);
	// Conversion ok?
	if(ok == false)
	{
	}

	// try connect
	bool bRet = CManageNetworkSingle::getInstance()->m_pClient->AttemptConnection(Server,RemotePort);

	// something went wrong? nag
	if(!bRet)
	{
		QMessageBox::warning(this, "Connect",
                                    "Could not connect to the <mumble> server.\n"
                                    "This program can't function correctly "
                                    "without the server.\n\n",
                                    "Retry",
                                    "Quit", 0, 0, 1);
		
	}
}

void ConfigDialog::AttemptServerStart()
{
	int Port;

	// convert port number
	QLineEdit* quallePort = ui->lineEditServerPort;	
	QString qsPort = quallePort->text();
	
	bool ok = false;
	Port = qsPort.toUShort(&ok,10);
	// Conversion ok?
	if(ok == false)
	{
	}

	bool bRet;
	bRet = CManageNetworkSingle::getInstance()->m_pServer->RunServer(Port);

	// something went wrong? nag
	if(!bRet)
	{
		QMessageBox::warning(this, "Create Server",
                                    "Could not create to the <mumble> server.\n"
                                    "This program can't function correctly "
                                    "without the server.\n\n",
                                    "Retry",
                                    "Quit", 0, 0, 1);
		
	}
}

void ConfigDialog::UpdateChatWindow(const char *strUpdate)
{
	emit SendUpdateGUI(strUpdate, ChatWin);
}

void ConfigDialog::UpdateServerLog(const char *strUpdate)
{
	emit SendUpdateGUI(strUpdate, ServerLog);
}

void ConfigDialog::saveConfig()
{
	QDir qdirDir;
	QFile qfileVideoConfigFile("config/video.cfg");
	QFile qfileInputConfigFile("config/input.cfg");
	QTextStream qtxtstrm;
	QString strTemp;

	qdirDir.mkpath("config"); // create the path

	qfileVideoConfigFile.open(QIODevice::WriteOnly);
	qtxtstrm.setDevice(&qfileVideoConfigFile);

	strTemp = ui->cbResolution->currentText();
	qtxtstrm << "strResolutionX" << " " << strTemp.mid(0, strTemp.indexOf("x") ) << "\n";
	qtxtstrm << "strResolutionY" << " " << strTemp.mid(strTemp.indexOf("x") + 1 ) << "\n";
	qtxtstrm << "bFullscreen" << " " << (ui->cbFullScreen->currentText() == strYes ? "true" : "false") << "\n";
	qtxtstrm << "strRenderSystem" << " " << ui->cbRender->currentText() << "\n";
	qtxtstrm << "bVSync" << " " << (ui->cbVSync->currentText() == strYes ? "true" : "false") << "\n";
	qtxtstrm << "iBPP" << " " << ui->cbBPP->currentText() << "\n";

	qfileVideoConfigFile.close();

	qfileInputConfigFile.open(QIODevice::WriteOnly);
	qtxtstrm.setDevice(&qfileInputConfigFile);
	qtxtstrm << "cForward" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(0,1)->widget())->text() << "\n";
	qtxtstrm << "cBackward" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(1,1)->widget())->text() << "\n";
	qtxtstrm << "cLeft" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(2,1)->widget())->text() << "\n";
	qtxtstrm << "cRight" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(3,1)->widget())->text() << "\n";
	qtxtstrm << "cUp" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(4,1)->widget())->text() << "\n";
	qtxtstrm << "cDown" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(5,1)->widget())->text() << "\n";
	qtxtstrm << "cRun" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(6,1)->widget())->text() << "\n";
	qtxtstrm << "cMenu" << " " << ((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(7,1)->widget())->text() << "\n";


	qfileInputConfigFile.close();
}

void ConfigDialog::setConfig(ConfigContainer *configContainer)
{
	QTextStream tsTemp;
	QString strTemp;

	tsTemp.setString(&strTemp);

	tsTemp << configContainer->videoConfig.iResolutionX << "x" << configContainer->videoConfig.iResolutionY;
	ui->cbResolution->setCurrentIndex( ui->cbResolution->findText(strTemp) );

	strTemp.clear();
	tsTemp << configContainer->videoConfig.iBPP;
	ui->cbBPP->setCurrentIndex( ui->cbBPP->findText(strTemp) );

	strTemp.clear();
	tsTemp << (configContainer->videoConfig.bFullscreen ? strYes : strNo); // Now thats one cool context for the "?"-operator!
	ui->cbFullScreen->setCurrentIndex( ui->cbFullScreen->findText(strTemp) );

	strTemp.clear();
	tsTemp << configContainer->videoConfig.carrRenderMode;
	ui->cbRender->setCurrentIndex( ui->cbRender->findText(strTemp) );

	strTemp.clear();
	tsTemp << (configContainer->videoConfig.bVSync ? strYes : strNo);
	ui->cbVSync->setCurrentIndex( ui->cbVSync->findText(strTemp) );

	// set input
	strTemp.clear();
	tsTemp << configContainer->keyMapping.carrForward;
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(0,1)->widget())->setText(strTemp);

	strTemp.clear();
	tsTemp << configContainer->keyMapping.carrBackward;
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(1,1)->widget())->setText(strTemp);

	strTemp.clear();
	tsTemp << configContainer->keyMapping.carrLeft;
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(2,1)->widget())->setText(strTemp);

	strTemp.clear();
	tsTemp << configContainer->keyMapping.carrRight;
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(3,1)->widget())->setText(strTemp);

	strTemp.clear();
	tsTemp << configContainer->keyMapping.carrUp;
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(4,1)->widget())->setText(strTemp);

	strTemp.clear();
	tsTemp << configContainer->keyMapping.carrDown;
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(5,1)->widget())->setText(strTemp);

	strTemp.clear();
	tsTemp << configContainer->keyMapping.carrRun;
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(6,1)->widget())->setText(strTemp);

	strTemp.clear();
	tsTemp << configContainer->keyMapping.carrMenu;
	((QLineEditEx*)ui->LayoutKeyLayout->itemAtPosition(7,1)->widget())->setText(strTemp);

}
